#coding=utf-8
# !/usr/bin/python
# -*- coding: utf-8 -*-

from com import * 

OCMD_UNVAILD = 0 # 无效 */
OCMD_PASSIVE = 1 # 1 开关输入检测 */
OCMD_PECAILIB = 2 # 2 压差传感器自动校准 */
OCMD_PESETKB = 3 # 3 压差传感器手动校准 */
OCMD_LEDTEST = 6 # 6 指示灯检测 */
OCMD_PVAVMA = 7 # 7 供氧阀电流控制 */
OCMD_PVAVFLOW = 8 # 8 供氧阀开度控制 */
OCMD_PVAVCAILIB = 9 # 9 供氧阀控制电流校准 */
OCMD_EXIT = 0xF # 15 退出部件检测 */


class Devmatain:
    def __init__(self, ui):
        self.ui = ui
        self.com = ui.com
        self.remote_power_on = 0
        self.matain_type = 0
        self.matain_ledlv = 0
        self.pvavle_ctl_ma1 = 0
        self.pvavle_ctl_ma2 = 0
        self.pvavle_ctl_flow1 = 0
        self.pvavle_ctl_flow2 = 0
        self.pvavle_ctl_cacal1 = 0
        self.pvavle_ctl_cacal2 = 0
        
        self.pvavle_pe_cacal1 = 0
        self.pvavle_pe_cacal2 = 0
        
        self.setup_matain(self.ui)
        
    def setup_matain(self, ui):
        ''' 比例阀检测 '''
        
        '''
        self.UISend_button1 = self.ui.pushButton_13 # 发送按钮1
        self.UIradioButton1_exit = self.ui.radioButton_3 # 退出检测1
        self.UIradioButton_ma = self.ui.radioButton_4
        self.UIradioButton_flow = self.ui.radioButton_5
        self.UIradioButton_cal = self.ui.radioButton_18 
        self.UIlineEdit_ma1 = self.ui.lineEdit_270
        self.UIlineEdit_ma2 = self.ui.lineEdit_271
        self.UIlineEdit_flow1 = self.ui.lineEdit_272
        self.UIlineEdit_flow2 = self.ui.lineEdit_273
        self.UIlineEdit_cal1 = self.ui.lineEdit_274
        self.UIlineEdit_cal2 = self.ui.lineEdit_275
        #self.UISend_button1.clicked.connect(self.ui_pushButton1_cb)
        '''
        
        ''' 指示灯检测 '''
        self.UISend_button2 = self.ui.pushButton_10 # 发送按钮2
        self.UISend_button2.clicked.connect(self.ui_pushButton2_cb)
        
        ''' 压差传感器校准 '''
        self.UISend_button3 = self.ui.pushButton_12 # 发送按钮3
        self.UISend_button3.clicked.connect(self.ui_pushButton3_cb)
        
        ''' 远程启动和透传 '''
        self.UISend_button4 = self.ui.pushButton_14 # 发送按钮4
        self.UISend_button4.clicked.connect(self.ui_pushButton4_cb)
        
    def ui_cb_sendmsg(self):
        if self.com.serdev.ser.isOpen():
            array = self.com.pack_frame_head(0x24, 0x30)
            self.pack_matain_msg(array)
            self.com.pack_frame_tail(array)
            self.com.serdev.ser.write(array)
            print(' '.join(f'{c:02x}' for c in array))
        else:
            pass
                    
    def ui_pushButton1_cb(self):
        if self.UIradioButton1_exit.isChecked():
            self.matain_type = OCMD_EXIT
            print("OCMD_EXIT")   
        elif self.UIradioButton_ma.isChecked():
            self.matain_type = OCMD_PVAVMA
            print("OCMD_PVAVMA")   
            self.pvavle_ctl_ma1 = str_to_int_safe(self.UIlineEdit_ma1.text())*10
            self.pvavle_ctl_ma2 = str_to_int_safe(self.UIlineEdit_ma2.text())*10
        elif self.UIradioButton_flow.isChecked():
            self.matain_type = OCMD_PVAVFLOW 
            print("OCMD_PVAVFLOW")    
            self.pvavle_ctl_flow1 = str_to_int_safe(self.UIlineEdit_flow1.text())
            self.pvavle_ctl_flow2 = str_to_int_safe(self.UIlineEdit_flow2.text())
        elif self.UIradioButton_flow.isChecked():
            self.matain_type = OCMD_PVAVFLOW 
            print("OCMD_PVAVFLOW")    
            self.pvavle_ctl_flow1 = str_to_int_safe(self.UIlineEdit_flow1.text())
            self.pvavle_ctl_flow2 = str_to_int_safe(self.UIlineEdit_flow2.text()) 
        elif self.UIradioButton_cal.isChecked():
            self.matain_type = OCMD_PVAVCAILIB 
            print("OCMD_PVAVCAILIB")    
            self.pvavle_ctl_cacal1 = str_to_int_safe(self.UIlineEdit_cal1.text())*10
            self.pvavle_ctl_cacal2 = str_to_int_safe(self.UIlineEdit_cal2.text())*10 
        self.ui_cb_sendmsg()     

    def ui_pushButton2_cb(self):
        self.UIradioButton2_exit = self.ui.radioButton_8 # 退出检测1
        self.UIradioButton2_led = self.ui.radioButton_7
  
        self.UIradioButton2_len0_on = self.ui.radioButton_9
        self.UIradioButton2_len0_ff = self.ui.radioButton_10
        self.UIradioButton2_len1_on = self.ui.radioButton_11
        self.UIradioButton2_len1_ff = self.ui.radioButton_12
        self.UIradioButton2_len2_on = self.ui.radioButton_16
        self.UIradioButton2_len2_ff = self.ui.radioButton_17
        self.UIradioButton2_len3_on = self.ui.radioButton_19
        self.UIradioButton2_len3_ff = self.ui.radioButton_13
        self.UIradioButton2_len4_on = self.ui.radioButton_20
        self.UIradioButton2_len4_ff = self.ui.radioButton_18

        if self.UIradioButton2_exit.isChecked():
            self.matain_type = OCMD_EXIT
        elif self.UIradioButton2_led.isChecked():
            self.matain_type = OCMD_LEDTEST
            if self.UIradioButton2_len0_on.isChecked():
                self.matain_ledlv = set_bit(self.matain_ledlv, 2) 
            else:
                self.matain_ledlv = clear_bit(self.matain_ledlv, 2)  

            if self.UIradioButton2_len1_on.isChecked():
                self.matain_ledlv = set_bit(self.matain_ledlv, 3) 
            else:
                self.matain_ledlv = clear_bit(self.matain_ledlv, 3)  

            if self.UIradioButton2_len2_on.isChecked():
                self.matain_ledlv = set_bit(self.matain_ledlv, 4) 
            else:
                self.matain_ledlv = clear_bit(self.matain_ledlv, 4) 
                
            if self.UIradioButton2_len3_on.isChecked():
                self.matain_ledlv = set_bit(self.matain_ledlv, 5) 
            else:
                self.matain_ledlv = clear_bit(self.matain_ledlv, 5)  

            if self.UIradioButton2_len4_on.isChecked():
                self.matain_ledlv = set_bit(self.matain_ledlv, 6) 
            else:
                self.matain_ledlv = clear_bit(self.matain_ledlv, 6)     
                


        self.ui_cb_sendmsg()     


    def ui_pushButton3_cb(self):
        self.UIradioButton_auto = self.ui.radioButton_4
        self.UIradioButton_manual = self.ui.radioButton_3
        self.UIlineEdit_pe_cacal1 = self.ui.lineEdit_268
        # self.UIlineEdit_pe_cacal2 = self.ui.lineEdit_269
        
        if self.UIradioButton_auto.isChecked():
            self.matain_type = OCMD_PECAILIB
            print("OCMD_PECAILIB")   
        elif self.UIradioButton_manual.isChecked():
            self.matain_type = OCMD_PESETKB
            self.pvavle_pe_cacal1 = str_to_int_safe(self.UIlineEdit_pe_cacal1.text())*10
            # self.pvavle_pe_cacal2 = str_to_int_safe(self.UIlineEdit_pe_cacal2.text())*10
            print("OCMD_PESETKB") 
        elif self.ui.radioButton.isChecked():
            print("8") 
            self.matain_type = 8
        elif self.ui.radioButton_2.isChecked():
            print("9") 
            self.matain_type = 9   
        elif self.ui.radioButton_5.isChecked():
            print("9") 
            self.matain_type = 10 
            
        self.ui_cb_sendmsg()  
        
    def ui_pushButton4_cb(self):
        self.UIradioButton_power_default = self.ui.radioButton_6
        self.UIradioButton_power_on = self.ui.radioButton_14
        self.UIradioButton_power_off = self.ui.radioButton_21

        
        if self.UIradioButton_power_default.isChecked():
            self.remote_power_on = 0
            self.matain_type = OCMD_EXIT
            print("UIradioButton_power_default") 
            self.ui_cb_sendmsg()  
        elif self.UIradioButton_power_on.isChecked():
            self.remote_power_on = 1
            self.matain_type = OCMD_EXIT
            print("UIradioButton_power_on") 
            self.ui_cb_sendmsg()     
        elif self.UIradioButton_power_off.isChecked():
            self.remote_power_on = 2
            self.matain_type = OCMD_EXIT
            print("UIradioButton_power_off")  
            self.ui_cb_sendmsg()


            
    def pack_matain_msg(self, array):
        array[36] = self.remote_power_on
        array[37] = self.com.dump_en
        array[38] = self.matain_type
        
        array[39] = self.pvavle_pe_cacal1 & 0xff
        array[40] = (self.pvavle_pe_cacal1 >> 8)& 0xff
        
        array[41] = self.pvavle_pe_cacal2 & 0xff
        array[42] = (self.pvavle_pe_cacal2 >> 8)& 0xff

        array[43] = self.matain_ledlv
        array[44] = self.pvavle_ctl_flow1 & 0xff
        array[45] = (self.pvavle_ctl_flow1 >> 8)& 0xff
        array[46] = self.pvavle_ctl_ma1 & 0xff
        array[47] = (self.pvavle_ctl_ma1 >> 8)& 0xff
        array[48] = self.pvavle_ctl_cacal1 & 0xff
        array[49] = (self.pvavle_ctl_cacal1 >> 8)& 0xff
        
        array[50] = self.pvavle_ctl_flow2 & 0xff
        array[51] = (self.pvavle_ctl_flow2 >> 8)& 0xff
        array[52] = self.pvavle_ctl_ma2 & 0xff
        array[53] = (self.pvavle_ctl_ma2 >> 8)& 0xff
        array[54] = self.pvavle_ctl_cacal2 & 0xff
        array[55] = (self.pvavle_ctl_cacal2 >> 8)& 0xff



